#!/usr/bin/python
'''
Combines multiple ham files.
'''
import sys
import ham
import numpy as np
import h5py

# parameters.
out_ham_file = sys.argv[1]

in_hams = []
for i in range(2,len(sys.argv)):
	in_hams.append(sys.argv[i])


# create combined structure.
print "create combined"
ham_comb = ham.create_array(10000000)
cmb_idx = 0

# open each for reading.
ham_objs = []
for ham_file in in_hams:
	
	# open dataset.
	ham_obj = ham.HamFile(ham_file, read=1)
	
	# read into memory.
	print "loading", ham_file
	ham_arr = ham_obj.load_complete()
	#ham_arr = ham_obj.data[0:10000]
	
	# assign good fields to combined.
	print "copying data."
	for i in range(ham_arr.size):
		if ham_arr[i]['QNAME'][0] == '': continue
		
		if cmb_idx >= ham_comb.size:
			print "Resizing"
			ham_comb.resize((ham_comb.size+1000000,1))
		
		ham_comb[cmb_idx] = ham_arr[i]
		cmb_idx += 1

	ham_obj.close()
	del ham_obj

	
# save data.
if cmb_idx < ham_comb.size:
	print "resize", cmb_idx, ham_comb.size
	ham_comb.resize((cmb_idx,1))
	
print "saving.", cmb_idx
ham_out = ham.HamFile(out_ham_file, read=0, cprs=True)
ham_out.save_complete(ham_comb)
ham_out.close()
